package com.blb.controller;

import java.io.IOException;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.blb.entity.Menu;
import com.blb.entity.Role;
import com.blb.entity.Rolemenu;
import com.blb.entity.User;
import com.blb.mapper.MenuExtMapper;
import com.blb.service.RoleService;
import com.blb.utils.DateUtils;
import com.blb.utils.LayuiPage;
import com.blb.utils.NowUserUtils;
import com.blb.utils.UUIDUtils;

@Controller
@RequestMapping("role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private MenuExtMapper menuExtMapper;
	
	/**
	 *  根据用户id获取它所拥有的按钮
	 * @param model
	 * @param httpsession
	 * @param menuid
	 * @return
	 * @throws IOException
	 */
	@RequiresPermissions("role:list")
	@RequestMapping("showList")
	public String showList(Model model,HttpSession httpsession,String menuid) throws IOException{
		//查询左侧菜单
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		List<Menu> list = menuExtMapper.getButtenByuserid(user.getId(),menuid);
		
		model.addAttribute("bts", list);
		return "/role/role_list";
	}
	
	
	/**
	 * 分页
	 * @param page
	 * @param limit
	 * @param rolename
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("page")
	public LayuiPage page(int page,int limit,String rolename) throws IOException{
		LayuiPage pagelist = roleService.page((page-1)*limit, limit, rolename);
		return pagelist;
	}
	
	@ResponseBody
	@RequestMapping("isExist")
	public String isExist(String rolename){
		try {
			List<Role> list = roleService.isExist(rolename);
			if(!list.isEmpty()){
				return "角色已存在";
			}
		} catch (IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return "";
	}
	
	
	/**
	 * 角色添加
	 * @param role
	 * @param session
	 * @return
	 */
	@RequiresPermissions("role:add")
	@ResponseBody
	@RequestMapping("add")
	public String add(Role role,HttpSession session,@Param("menu") String menu){
		role.setId(UUIDUtils.getID());
		role.setCreateTime(DateUtils.gettime());
		
		User user = NowUserUtils.getNowUser();
		role.setCreateUser(user.getUsername());
		try {
			String msg = roleService.add(role,menu);
			return msg;
		} catch (IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	


	
	/**
	 * 修改角色
	 * @param role
	 * @param session
	 * @return
	 */
	@RequiresPermissions("role:update")
	@ResponseBody
	@RequestMapping("update")
	public String update(Role role,HttpSession session,@Param("menu")String menu){
		role.setUpdateTime(DateUtils.gettime());
		User user = (User) session.getAttribute("user");
		role.setUpdateUser(user.getUsername());
		System.out.println(role.getRolename());
		System.out.println(role.getNote());
		try {
			String msg = roleService.update(role,menu);
			return msg;
		} catch (IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
	/**
	 * 删除角色
	 * @param rolesIds
	 * @return
	 */
	@RequiresPermissions("role:delete")
	@ResponseBody
	@RequestMapping("delete")
	public String delete(@RequestBody List<String> rolesIds){
		try {
			String msg = roleService.delete(rolesIds);
			return msg;
		} catch (IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
	/**
	 * 添加页面获取父级菜单
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("showAdd")
	public String getParentMenu(Model model) throws IOException{
		List<Map<String,Object>> list = roleService.getParentMenu();
		String menus="";
		if(!list.isEmpty()){
			menus = JSON.toJSON(list).toString();
		}
		model.addAttribute("menus", menus);
		return "role/role_add";
	}
	
	/**
	 * 修改页面获取父级菜单
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("showUpdate")
	public String UpdategetParentMenu(Model model,Role role) throws IOException{
		
		//菜单名
		List<Map<String,Object>> list = roleService.getParentMenu();
		String menus="";
		if(!list.isEmpty()){
			menus = JSON.toJSON(list).toString();
		}
		
		//当前角色的菜单
		List<Rolemenu> roleMenuList = roleService.getMenuByrole(role);
		String roleMenu = "";
		if(!roleMenuList.isEmpty()){
			roleMenu = JSON.toJSON(roleMenuList).toString();
		}
		
		model.addAttribute("role", role);
		model.addAttribute("roleMenu", roleMenu);
		model.addAttribute("menus", menus);
		return "role/role_update";
	}
}
